//! 主动扫描配置

use serde::{Deserialize, Serialize};
use std::time::Duration;

/// 主动扫描配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActiveScanConfig {
    /// 爬虫配置
    pub crawler: CrawlerConfig,
    /// 扫描配置（复用被动扫描配置）
    pub scan: ScanConfig,
}

impl Default for ActiveScanConfig {
    fn default() -> Self {
        Self {
            crawler: CrawlerConfig::default(),
            scan: ScanConfig::default(),
        }
    }
}

/// 爬虫配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrawlerConfig {
    /// 最大爬取深度
    pub max_depth: usize,
    /// 最大页面数量
    pub max_pages: usize,
    /// 并发请求数
    pub concurrency: usize,
    /// 请求超时时间（秒）
    pub timeout_secs: u64,
    /// User-Agent
    pub user_agent: String,
    /// 是否跟随重定向
    pub follow_redirects: bool,
    /// 最大重定向次数
    pub max_redirects: usize,
    /// 是否启用JavaScript渲染
    pub enable_js: bool,
    /// 是否爬取外部链接
    pub crawl_external: bool,
    /// 忽略的扩展名（不爬取）
    pub ignore_extensions: Vec<String>,
    /// 自定义请求头
    pub custom_headers: std::collections::HashMap<String, String>,
    /// 是否启用 Fuzz 模式
    pub enable_fuzz: bool,
    /// Fuzz 字典路径
    pub fuzz_dict_path: Option<String>,
}

impl Default for CrawlerConfig {
    fn default() -> Self {
        Self {
            max_depth: 3,
            max_pages: 1000,
            concurrency: 10,
            timeout_secs: 30,
            user_agent: "Good-MITM-ActiveScan/0.1.0".to_string(),
            follow_redirects: true,
            max_redirects: 5,
            enable_js: false,
            crawl_external: false,
            ignore_extensions: vec![
                "css".to_string(),
                "js".to_string(),
                "jpg".to_string(),
                "jpeg".to_string(),
                "png".to_string(),
                "gif".to_string(),
                "ico".to_string(),
                "svg".to_string(),
                "woff".to_string(),
                "woff2".to_string(),
                "ttf".to_string(),
                "eot".to_string(),
                "pdf".to_string(),
                "zip".to_string(),
                "rar".to_string(),
                "tar".to_string(),
                "gz".to_string(),
            ],
            custom_headers: std::collections::HashMap::new(),
            enable_fuzz: false,
            fuzz_dict_path: None,
        }
    }
}

/// 扫描配置（简化版，复用被动扫描的配置）
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScanConfig {
    /// 是否启用指纹识别
    pub enable_fingerprinting: bool,
    /// 是否启用去重
    pub enable_dedup: bool,
    /// 去重TTL（秒）
    pub dedup_ttl: u64,
    /// 工作线程数
    pub worker_count: usize,
    /// 事件队列大小
    pub queue_size: usize,
    /// 扫描结果输出目录
    pub output_dir: String,
}

impl Default for ScanConfig {
    fn default() -> Self {
        Self {
            enable_fingerprinting: true,
            enable_dedup: true,
            dedup_ttl: 300,
            worker_count: num_cpus::get(),
            queue_size: 10_000,
            output_dir: "./active_scan_results".to_string(),
        }
    }
}

/// 配置加载和保存
impl ActiveScanConfig {
    /// 从文件加载配置
    pub fn from_file(path: &str) -> Result<Self, anyhow::Error> {
        let content = std::fs::read_to_string(path)?;
        let config: Self = serde_yaml::from_str(&content)?;
        Ok(config)
    }

    /// 保存配置到文件
    pub fn save_to_file(&self, path: &str) -> Result<(), anyhow::Error> {
        let content = serde_yaml::to_string(self)?;
        std::fs::write(path, content)?;
        Ok(())
    }
}
